package cn.lds.j375.healthexam.controller;

import cn.lds.j375.healthexam.dto.ResultDTO;
import cn.lds.j375.healthexam.entity.ExamRecord;
import cn.lds.j375.healthexam.entity.User;
import cn.lds.j375.healthexam.service.ExamRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Objects;


@Controller
@RequestMapping("exam_record")
public class ExamRecordController {

    private static final Logger log = LoggerFactory.getLogger(ExamRecordController.class);

    @Autowired
    private ExamRecordService examRecordService;

    @GetMapping("confirm_exam_by")
    public String confirmExam(int appointId, Model model){
        log.info("预约id:{}",appointId);

        model.addAttribute("appointId",appointId);
        return "confirm_exam";
    }

    @PostMapping("do_confirm")
    @ResponseBody
    public ResultDTO doConfirmExam(@RequestBody ExamRecord examRecord, HttpSession session){

        User loginUser = (User) session.getAttribute("loginUser");
        examRecord.setCreateBy(loginUser.getCreateBy());
        examRecord.setCreateTime(new Date());

        ResultDTO resultDTO = examRecordService.addExamRecord(examRecord);
        log.info("exam_record:{}",examRecord);
        if (resultDTO.getCode().equals(4000)){
            return ResultDTO.buildError(resultDTO.getInfo());
        }

        return ResultDTO.buildSuccess();
    }
}
